//
//  ViewController.m
//  IntelligentHousehold
//
//  Created by wmwMAC on 17/6/16.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "ViewController.h"

#import "NSString+LBKJExtenison.h"
#import "NSString+StringToWords.h"
#import "UIButton+BackgroundColor.h"

//#import "FMDBHelp.h"//数据库
//#import <FMDB/FMDB.h>

#import "DataBaseManager.h"//数据库2

#import <MessageUI/MessageUI.h>//系统短信

@interface ViewController ()<MFMessageComposeViewControllerDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //
//    instancetype *typeinst =
    [HttpRequest sharedInstance];
    
    [HttpRequest initiii];
    
    UIButton *iOSDevTip = [UIButton buttonWithType:UIButtonTypeCustom];
    iOSDevTip.frame = CGRectMake(20, 80, 100, 50);
    [iOSDevTip setBackgroundColor:[UIColor orangeColor]];
    [iOSDevTip setTitle:@"send msg" forState:UIControlStateNormal];
    [iOSDevTip setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:iOSDevTip];
    [iOSDevTip addTarget:self action:@selector(actioniOSDevTipClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [[DataBaseManager shareInstance]openDB];
    [[DataBaseManager shareInstance]createTable];
    
    
    NSArray *allArr = [[DataBaseManager shareInstance] selectAll];
    NSLog(@"%@",allArr);
    
    
    
    [self Home];
    
}



-(void)Home{

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"13750891239" forKey:@"tell"];
    [dic setObject:@"123456" forKey:@"password"];
    
    
    [HttpRequest postWithURLString:@"http://192.168.188.89/anju/Home/User/login" parameters:dic success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        
    } failure:^(NSError *error) {
        
    }
     ];
    

    
}


- (void)actioniOSDevTipClick:(id)sender
{
    //方法一
    //    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10086"]];
    
    //方法二  推荐 媳妇 么么哒
    [self showMessageView:[NSArray arrayWithObjects:@"10086", nil] title:@"xh" body:@" 媳妇 么么哒"];
    
    
    [[DataBaseManager shareInstance] insertTel:@"10086" Time:@"1990"];
}

-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
    if( [MFMessageComposeViewController canSendText] )
    {
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.recipients = phones;
        controller.navigationBar.tintColor = [UIColor redColor];
        controller.body = body;
        controller.messageComposeDelegate = self;
        
        
        [self presentViewController:controller animated:YES completion:nil];
                [[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                        message:@"该设备不支持短信功能"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil, nil];
        [alert show];
    }
}

#pragma mark - MFMessageComposeViewController

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    switch (result) {
        case MessageComposeResultSent:
            //信息传送成功
            
            break;
        case MessageComposeResultFailed:
            //信息传送失败
            
            break;
        case MessageComposeResultCancelled:
            //信息被用户取消传送
            
            break;
        default:
            break;
    }
}



-(void)dismissAlert:(UIAlertController *)al{
    [al dismissViewControllerAnimated:YES completion:nil];
}






-(void)openNetFMDB{
//    //    打开数据库
//    [self openDB];
//    //    在子线程中执行数据库插入操作
//    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
//        [self threadNoTransaction];
//    });
}

/*
#pragma mark - 数据存储文件的路径
- (NSString *)dbPath {
    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *stringPath = [string stringByAppendingPathComponent:@"test.sqlite"];
    NSLog(@"%@",stringPath);
    return stringPath;
}

#pragma mark - 打开数据库并建表
- (void)openDB {
    FMDatabase *database = [FMDatabase databaseWithPath:[self dbPath]];
    //打开数据库,如果数据库打开,建表,如果失败就返回错误信息
    if([database open]) {
        //建表
        BOOL isSuccess = [database executeUpdate:@"create table if not exists stu(name text)"];
        if (isSuccess) {
            NSLog(@"建表成功");
        } else {
            NSLog(@"建表失败");
        }
    } else {
        NSLog(@"打开数据库失败");
    }
}

#pragma mark - 多线程操作数据库时,非事务的处理方式
// 事务（Transaction）是并发控制的基本单位。所谓的事务，它是一个操作序列，这些操作要么都执行，要么都不执行，它是一个不可分割的工作单位。例如，银行转账工作：从一个账号扣款并使另一个账号增款，这两个操作要么都执行，要么都不执行。所以，应该把它们看成一个事务。事务是数据库维护数据一致性的单位，在每个事务结束时，都能保持数据一致性

- (void)threadNoTransaction {
    //数据库文件的路径
    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:[self dbPath]];
    //将一组操作添加到非事务处理中
    [queue inDatabase:^(FMDatabase *db) {
        //将操作放入事务中,加入事务操作
        [db beginTransaction];
        
        BOOL isError = NO;
        int temp = -1;
        for (int i = 0; i < 10000; i++) {
            isError = [db executeUpdate:@"insert into stu values(?)",@(i)];
            if (!isError) {//说明inError == NO,插入有问题
                if(temp == -1) {
                    temp = i;
                }
            }
        }
        if (isError) {
            NSLog(@"所有插入动作成功");
        } else {
            NSLog(@"所有插入动作失败---%d",temp);
        }
        //提交事务
        [db commit];
    }];
}

#pragma mark - 多线程操作数据库时,事务的处理方式
- (void)threadTransaction {
    //创建队列
    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:[self dbPath]];
    __block BOOL whoopsSomethingWrongHappened = true;
    //把任务包装到事务里
    [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        whoopsSomethingWrongHappened &= [db executeUpdate:@"insert into myTable values(?)",[NSNumber numberWithInt:1]];
        whoopsSomethingWrongHappened &= [db executeUpdate:@"insert into myTable values(?)",[NSNumber numberWithInt:2]];
        whoopsSomethingWrongHappened &= [db executeUpdate:@"insert into myTable values(?)",[NSNumber numberWithInt:3]];
        //如果有错误 返回
        if(!whoopsSomethingWrongHappened) {
            *rollback = YES;
            return;
        }
    }];
}

*/

-(void)btnBackColor{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(100, 100, 100, 100)];
    btn.backgroundColor = [UIColor redColor];
    [btn setBackgroundColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [btn setCornerBackgroundColor:[UIColor cyanColor] withRadius:10.0 forState:UIControlStateNormal];
    [self.view addSubview:btn];
}

-(void)zifuChaifenStr{
    NSString *str = @"sdjlkas;dlkjflsfjweoijfsla;fndf你打;的开发罗克韦尔叫法;放假健康代理商;安防监控啦;司法局";
    NSArray *arr = [str words];
    NSArray *arr2 = [str componentsSeparatedByString:@","];
    NSLog(@"%@",arr2);
    NSLog(@"%@",arr);
}

-(void)strWidth{
    NSString *str = @"sdjlkas;dlkjflsfjweoijfsla;fndf你打;的开发罗克韦尔叫法;放假健康代理商;安防监控啦;司法局";
    CGSize size1 = [str sizeWithFont:[UIFont systemFontOfSize:15]];
    CGSize size2 = [str sizeWithFont:[UIFont systemFontOfSize:15] andMaxW:200];
    
    
    UILabel *lable1 = [[UILabel alloc ] initWithFrame:CGRectMake(0, 50, size1.width, size1.height)];
    lable1.backgroundColor = [UIColor yellowColor];
    lable1.font = [UIFont systemFontOfSize:15];
    lable1.numberOfLines = 0;
    lable1.text = str;
    [self.view addSubview:lable1];
    
    UILabel *lable2 = [[UILabel alloc ] initWithFrame:CGRectMake(100, 100, size2.width, size2.height)];
    lable2.backgroundColor = [UIColor redColor];
    lable2.font = [UIFont systemFontOfSize:15];
    lable2.numberOfLines = 0;
    lable2.text = str;
    [self.view addSubview:lable2];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
